//
//  HRZTabBarController.m
//  EMarket
//
//  Created by 石头 on 2018/5/7.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZTabBarController.h"

// Controllers
#import "HRZNavigationController.h"
#import "HRZLoginViewController.h"

#import "HRZCommodityViewController.h"
// Models

// Views

// Vendors

// Categories

// Others

@interface HRZTabBarController ()<UITabBarControllerDelegate>

@property (nonatomic, strong) NSMutableArray *tabBarItems;

@end

@implementation HRZTabBarController

#pragma mark - LazyLoad
- (NSMutableArray *)tabBarItems{
    if (!_tabBarItems) {
        _tabBarItems = [[NSMutableArray alloc] init];
    }
    return _tabBarItems;
}


#pragma mark - LifeCyle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    WEAKSELF
    [[NSNotificationCenter defaultCenter] addObserverForName:LOGINOFFSELECTCENTERINDEX object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        weakSelf.selectedViewController = [weakSelf.viewControllers objectAtIndex:HRZTabBarControllerType_Home];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    self.delegate = self;
    
    [self addHrzChildViewContorller];
    
    self.selectedViewController = [self.viewControllers objectAtIndex:HRZTabBarControllerType_Home];
    
}

#pragma mark - Public

#pragma mark - 添加子控制器
- (void)addHrzChildViewContorller {
    NSArray *childArray = @[
                            @{MallClassKey  : @"HRZHomeViewController",// HRZHomeRecommendPageViewController
                              MallTitleKey  : @"首页",
                              MallImgKey    : @"tabr_02_up",
                              MallSelImgKey : @"tabr_02_down"},
                            
                            @{MallClassKey  : @"HRZCommodityViewController",
                              MallTitleKey  : @"分类",
                              MallImgKey    : @"tabr_02_up",
                              MallSelImgKey : @"tabr_02_down"},
                            
                            @{MallClassKey  : @"HRZShoppingCartViewController",
                              MallTitleKey  : @"购物车",
                              MallImgKey    : @"tabr_02_up",
                              MallSelImgKey : @"tabr_02_down"},
                            
                            @{MallClassKey  : @"HRZMyCenterViewController",
                              MallTitleKey  : @"我的",
                              MallImgKey    : @"tabr_05_up",
                              MallSelImgKey : @"tabr_05_down"},
                            ];
    //  HRZMyCenterViewController
    [childArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        UIViewController *controller = [NSClassFromString(dict[MallClassKey]) new];
        
        controller.title = dict[MallTitleKey];
        controller.tabBarItem.title = dict[MallTitleKey];//跟上面一样效果
        controller.tabBarItem.image = [[UIImage imageNamed:dict[MallImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        controller.tabBarItem.selectedImage = [[UIImage imageNamed:dict[MallSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        //未选中字体颜色
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:KBlackColor,NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateNormal];
        
        //选中字体颜色
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CNavBgColor,NSFontAttributeName:SYSTEMFONT(10.0f)} forState:UIControlStateSelected];
        //包装导航控制器
        
        HRZNavigationController *nav = [[HRZNavigationController alloc] initWithRootViewController:controller];
//        UITabBarItem *item = nav.tabBarItem;
//        item.image = [UIImage imageNamed:dict[MallImgKey]];
//        item.selectedImage = [[UIImage imageNamed:dict[MallSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//        item.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
        
        [self addChildViewController:nav];
        
//        [self.tabBarItems addObject:vc.tabBarItem];
        
    }];
}

#pragma mark - 控制器跳转拦截
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return YES;
    if (viewController == [tabBarController.viewControllers objectAtIndex:HRZTabBarControllerType_Person]) {
        if (![[HRZObjManager hrz_readUserDataForKey:@"isLogin"] isEqualToString:@"1"]) {
            
            HRZLoginViewController *hrzLoginVc = [HRZLoginViewController new];
            [self presentViewController:hrzLoginVc animated:YES completion:nil];
            return NO;
        }
    }
    return YES;
}

#pragma mark - Delegate
#pragma mark - UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [self tabBarButtonClick:[self getTabBarButton]];
}

- (UIControl *)getTabBarButton{
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [tabBarButtons addObject:tabBarButton];
        }
    }
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
    
    return tabBarButton;
}


#pragma mark - 点击动画
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据自己需求改动
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.3;
            animation.calculationMode = kCAAnimationCubic;
            //添加动画
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
    
}

@end
